﻿using AutoMapper;
using MediatR;
using Reporting.API.Application.CommandHandler.Productionorders;
using Reporting.API.Application.Commands.ForeignKey;
using Reporting.Domain.ForeignKey;
using Reporting.Errorcode.ErrorCode;
using Reporting.Infrastructure.Interfaces.Productionorders;
using Reporting.Infrastructure.Interfaces.ProductTypes;

namespace Reporting.API.Application.CommandHandler.ForeignKey
{
    /// <summary>
    /// 产品类型
    /// </summary>
    public class ProductTypeCommondHandler : IRequestHandler<ProductTypeCommond, ApiResult<IList<ProductType>>>
    {
        private readonly IProductTypeRepository  _productTypeRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<ProductTypeCommondHandler> _logger;

        public ProductTypeCommondHandler(ILogger<ProductTypeCommondHandler> logger, IMapper mapper, IProductTypeRepository productTypeRepository)
        {
            _logger = logger;
            _mapper = mapper;
            _productTypeRepository = productTypeRepository;
        }

        public async Task<ApiResult<IList<ProductType>>> Handle(ProductTypeCommond request, CancellationToken cancellationToken)
        {
            try
            {
                // 获取所有
                var ProductionPlans = await _productTypeRepository.GetAllAsync();

                var result = _mapper.Map<IList<ProductType>>(ProductionPlans);

                // 返回成功结果
                return ApiResult<IList<ProductType>>.Success(result, StatusCodeEnum.Success);
            }
            catch (Exception ex)
            {

                // 返回失败结果
                return ApiResult<IList<ProductType>>.Fail("查询失败：" + ex.Message, StatusCodeEnum.BadRequest);
            }
        }
    }
}
